package idv.arthur.work;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class Widgets {

	public final int ADD = 1;					//新增
	public final int DELETE = 2;				//刪除
	public final int UPDATE = 3;			//修改
	public final int INQUERY = 4;			//查詢
	
	public final int BSS = 1;					//系統
	public final int SUBSYS = 2;				//子系統
	public final int ENV = 3;					//環境
	public final int SQUAD = 4;				//部署班
	public final int HOST = 5;				//主機
	public final int PERSONNEL = 6;	//人員
	public final int REQUIREMENT=7;//需求
	
	public void keepLogProfile(int profileType,int actionType,String loginUser,Connection conn, PreparedStatement ps ) throws SQLException {
		ps = conn.prepareStatement("INSERT INTO  log_profile (typeID,type,creator,createdDateTime) VALUES (?,?,?,?)");
		ps.setInt(1, profileType);
		ps.setInt(2, actionType);
		ps.setString(3, loginUser);
		ps.setTimestamp(4, currentTimeStamp());
		ps.execute();
	}
	
	/**
	 * @將輸入的日期，轉成字串格式傳回
	 * @創建時間：2012/9/24 下午2:47:20
	 * @創建人員：郭啟揚 (842297)
	 */
	public String changeTimeFormat(Date dt, String format) {
		SimpleDateFormat sdf = new java.text.SimpleDateFormat(format);
		return sdf.format(dt);
	}

	/**
	 * 	將輸入的日期，轉成字串格式傳回
	 *		創建時間：2012/9/28 上午11:08:13
	 *		創建人員：郭啟揚 (842297)
	 */
	public String changeTimeFormat(Date dt) {
		SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(dt);
	}
	
	/**
	 *		創建時間：2012/9/24 下午2:51:38
	 *		創建人員：郭啟揚 (842297)
	 */
	public Connection getConn(boolean isWeb) {
		Connection con = null;

		if (isWeb) {
			try {
				Context ctx = new InitialContext();
				String strLookup = "java:/env/jdbc/DPS";
				DataSource ds = (DataSource) ctx.lookup(strLookup);
				con = ds.getConnection();
			} catch (NamingException e) {
				System.out.println("建立連線時發生錯誤" + e.toString());
			} catch (SQLException e) {
				System.out.println("建立連線時發生錯誤" + e.toString());
			}
		} else {
			try {
				Class.forName("com.mysql.jdbc.Driver");
				// 註冊driver
				con = DriverManager.getConnection("jdbc:mysql://localhost:3306/dps", "p_dps","p_dps1234");
			} catch (ClassNotFoundException e) {
				System.out.println("DriverClassNotFound :" + e.toString());
			} catch (SQLException x) {
				System.out.println("Exception :" + x.toString());
			}
		}

		return con;
	}
	
	/**
	 * 	回傳依指定格式下，目前的日期與時間
	 *		創建時間：2012/9/24 下午2:53:28
	 *		創建人員：郭啟揚 (842297)
	 */
	public Timestamp currentTimeStamp(String format){
		java.util.Date dt = new java.util.Date();
		java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(format);
	    String currentTime = sdf.format(dt);
	    return Timestamp.valueOf(currentTime);
	}
	
	/**
	 * 	回傳目前的日期與時間
	 *		創建時間：2012/9/26 上午9:23:58
	 *		創建人員：郭啟揚 (842297)
	 */
	public Timestamp currentTimeStamp(){
		java.util.Date dt = new java.util.Date();
	    java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	    String currentTime = sdf.format(dt);
	    return Timestamp.valueOf(currentTime);
	}
	
	public String errProcess (Exception e) {
		String mes = "";
	    mes += e.toString() + "\n\n";
		for (int i=0; i<e.getStackTrace().length;i++) {
			mes += "檔案名稱："+e.getStackTrace()[i].getFileName() + "\n";
			mes += "所在行數："+e.getStackTrace()[i].getLineNumber() + "\n";
			mes += "方法名稱："+e.getStackTrace()[i].getMethodName() + "\n";
			mes += "類別名稱："+e.getStackTrace()[i].getClassName() + "\n\n";
		}
		
		return mes;
	}
	
}
